package com.jwthhealth_pub.old.report.view.adapter;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.old.report.model.resbody.SportSuggListResp;
import com.jwthhealth_pub.old.report.view.SportSuggestActivity;

import java.util.List;

/**
 * 运动建议描述
 * Created by liuzhen on 2017/11/2.
 */

public class SportSuggDescAdapter extends RecyclerView.Adapter {
    private final int minLine = 2;

    private final SportSuggestActivity mActivity;
    private final List<SportSuggListResp.DataBeanX.DescBean> mData;

    public SportSuggDescAdapter(SportSuggestActivity sportSuggestActivity, List<SportSuggListResp.DataBeanX.DescBean> desc) {
        this.mActivity = sportSuggestActivity;
        this.mData = desc;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(View.inflate(mActivity, R.layout.item_desc_sport_sugg_list, null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ViewHolder vh = (ViewHolder) holder;

        SportSuggListResp.DataBeanX.DescBean descBean = mData.get(position);
        if (descBean == null) return;

        if (descBean.getType() != null)
            vh.title.setText(descBean.getType());
        if (descBean.getDesc() != null)
            vh.desc.setText(descBean.getDesc());

        if (!vh.isShow) {
            vh.desc.setMaxLines(minLine);
            vh.desc.setEllipsize(TextUtils.TruncateAt.END);
        }

        vh.arrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (vh.isShow) {
                    vh.isShow = false;
                    vh.arrow.setBackground(mActivity.getResources().getDrawable(R.mipmap.show_more_arrow));
                    vh.desc.setMaxLines(minLine);
                    vh.desc.setEllipsize(TextUtils.TruncateAt.END);
                } else {
                    vh.isShow = true;
                    vh.arrow.setBackground(mActivity.getResources().getDrawable(R.mipmap.show_preview_arrow));
                    // TODO: 2017/11/8 自适应行数
                    vh.desc.setMaxLines(100);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mData != null && mData.size() > 0)
            return mData.size();
        else
            return 0;
    }

    private class ViewHolder extends RecyclerView.ViewHolder {

        TextView title, desc;
        ImageView arrow;

        boolean isShow;

        public ViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            desc = itemView.findViewById(R.id.desc);
            arrow = itemView.findViewById(R.id.iv_arrow);
        }
    }
}
